package com.gs.api.projects.bzapplets.web;

import cn.hutool.core.bean.BeanUtil;
import com.gs.api.projects.bzapplets.dao.BzDomainDao;
import com.gs.api.projects.bzapplets.entity.BzAllWallpaper;
import com.gs.api.projects.bzapplets.entity.BzClassification;
import com.gs.api.projects.bzapplets.entity.BzWallpaper;
import com.gs.api.projects.bzapplets.service.BzAllWallpaperService;
import com.gs.api.projects.bzapplets.service.BzClassificationService;
import com.gs.api.projects.bzapplets.service.BzDomainService;
import com.gs.api.projects.bzapplets.service.BzWallpaperService;
import com.gs.api.core.Result;
import com.gs.api.reptile.entity.WallpaperData;
import com.gs.api.reptile.xiaomi.XiaomiCollectData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;


/**
 * @author guoyunfei on 2021/9/4 3:24
 * @className CollectWallpaperController
 * @description TODO 小米主题壁纸采集数据接口
 * @Version 1.0
 */

@Api(tags = "壁纸采集数据接口")
@RestController
@RequestMapping("{appid}/collectData")
public class CollectWallpaperController {

    //TODO 开始页数
    private static int startPage = 0;

    @Autowired
    private BzClassificationService bzClassificationService;

    @Autowired
    private BzWallpaperService bzWallpaperService;

    @Autowired
    private BzDomainDao bzDomainDao;

    @Autowired
    private BzDomainService bzDomainService;

    @Autowired
    private BzAllWallpaperService bzAllWallpaperService;

    @ApiOperation(value = "采集壁纸数据(小米)", notes="采集壁纸数据(小米)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "count", value = "采集次数", required = true, dataType = "Integet"),
            @ApiImplicitParam(name = "pageSize", value = "开始页数", required = false, dataType = "Integet"),
            //@ApiImplicitParam(name = "keyword", value = "分类", required = true, dataType = "String"),
            @ApiImplicitParam(name = "className", value = "分类名称", required = true, dataType = "String"),
            @ApiImplicitParam(name = "appid", value = "小程序appid", required = true, dataType = "String"),
    })
    @GetMapping("/data")
    public Result getData(Integer count,Integer pageSize,String className,@PathVariable String appid){

        BzClassification classification = bzClassificationService.selectOne(className,appid);
        if(null == classification){
            return Result.failure("系统找不到改分类、请重新输入");
        }

        if(null != pageSize){
            startPage = pageSize;
        }


        Collection<BzWallpaper> bw = new ArrayList<>();

        for (int i = 0; i < count; i++) {

            List<WallpaperData> dataList = XiaomiCollectData.getWallpapers(className,startPage);

            if(null == dataList || dataList.size() == 0){
                return Result.success("采集失败，此分类数据采集获取失败");
            }

            startPage++;
            for (WallpaperData data : dataList){
                BzWallpaper w = new BzWallpaper();
                w.setTitle(data.getTitle());
                w.setName(data.getName());
                w.setThumbnail(data.getImageUrl().replace("webp","png"));
                w.setImage(data.getOriginalImageUrl().replace("webp","png"));
                w.setClassId(classification.getId());
                w.setTags(classification.getName());
                w.setAppid(appid);
                w.setIsDelete(1);
                bw.add(w);
            }

        }
        //过滤重复数据
        Collection<BzWallpaper> list = bw.stream().distinct().collect(Collectors.toList());
        bzWallpaperService.saveBatch(list);

        // TODO 数据同步至其它表
        Collection<BzAllWallpaper> all = new ArrayList<>();
        //拷贝数据
        list.stream().forEach(wallpaper ->{
            BzAllWallpaper bzAllWallpaper = new BzAllWallpaper();
            BeanUtil.copyProperties(wallpaper,bzAllWallpaper);
            all.add(bzAllWallpaper);
        });
        bzAllWallpaperService.saveBatch(all);
        return Result.success("采集成功条数："+list.size());
    }



}
